#include <gtest\gtest.h>
#include <Zaf\Net\Convert.h>



TEST(Net, ConvertToString) {

	std::string str = Zaf::Net::ToString<char>(0);
	ASSERT_EQ("0.0.0.0", str);

	str = Zaf::Net::ToString<char>(-1);
	ASSERT_EQ("255.255.255.255", str);

	str = Zaf::Net::ToString<char>(0x01020304);
	ASSERT_EQ("4.3.2.1", str);
}


TEST(Net, ConvertToWideString) {

	std::wstring str = Zaf::Net::ToString<wchar_t>(0);
	ASSERT_EQ(L"0.0.0.0", str);

	str = Zaf::Net::ToString<wchar_t>(-1);
	ASSERT_EQ(L"255.255.255.255", str);

	str = Zaf::Net::ToString<wchar_t>(0x01020304);
	ASSERT_EQ(L"4.3.2.1", str);
}


TEST(Net, ConvertFromString) {

	std::string str("0.0.0.0");
	unsigned int addr = Zaf::Net::FromString(str);
	ASSERT_EQ(0, addr);
	addr = Zaf::Net::FromString("0.0.0.0");
	ASSERT_EQ(0, addr);

	str = "255.255.255.255";
	addr = Zaf::Net::FromString(str);
	ASSERT_EQ(-1, addr);
	addr = Zaf::Net::FromString("255.255.255.255");
	ASSERT_EQ(-1, addr);

	str = "6.7.8.10";
	addr = Zaf::Net::FromString(str);
	ASSERT_EQ(0x0a080706, addr);
	addr = Zaf::Net::FromString("6.7.8.10");
	ASSERT_EQ(0x0a080706, addr);

	str = "abc";
	addr = Zaf::Net::FromString(str);
	ASSERT_EQ(-1, addr);
	addr = Zaf::Net::FromString("def");
	ASSERT_EQ(-1, addr);

	//Secure version
	str = "1.2.3.4";
	bool result = Zaf::Net::FromString(str, addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(0x04030201, addr);
	result = Zaf::Net::FromString("1.2.3.4", addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(0x04030201, addr);

	str = "255.255.255.255";
	result = Zaf::Net::FromString(str, addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(-1, addr);
	result = Zaf::Net::FromString("255.255.255.255", addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(-1, addr);

	str = "abcdef";
	addr = 333;
	result = Zaf::Net::FromString(str, addr);
	ASSERT_EQ(false, result);
	ASSERT_EQ(333, addr);
	result = Zaf::Net::FromString("abcef", addr);
	ASSERT_EQ(false, result);
	ASSERT_EQ(333, addr);
}



TEST(Net, ConvertFromWideString) {

	std::wstring str(L"0.0.0.0");
	unsigned int addr = Zaf::Net::FromString(str);
	ASSERT_EQ(0, addr);
	addr = Zaf::Net::FromString(L"0.0.0.0");
	ASSERT_EQ(0, addr);

	str = L"255.255.255.255";
	addr = Zaf::Net::FromString(str);
	ASSERT_EQ(-1, addr);
	addr = Zaf::Net::FromString(L"255.255.255.255");
	ASSERT_EQ(-1, addr);

	str = L"6.7.8.10";
	addr = Zaf::Net::FromString(str);
	ASSERT_EQ(0x0a080706, addr);
	addr = Zaf::Net::FromString(L"6.7.8.10");
	ASSERT_EQ(0x0a080706, addr);

	str = L"abc";
	addr = Zaf::Net::FromString(str);
	ASSERT_EQ(-1, addr);
	addr = Zaf::Net::FromString(L"sdf");
	ASSERT_EQ(-1, addr);

	//Secure version
	str = L"1.2.3.4";
	bool result = Zaf::Net::FromString(str, addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(0x04030201, addr);
	result = Zaf::Net::FromString(L"1.2.3.4", addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(0x04030201, addr);

	str = L"255.255.255.255";
	result = Zaf::Net::FromString(str, addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(-1, addr);
	result = Zaf::Net::FromString(L"255.255.255.255", addr);
	ASSERT_EQ(true, result);
	ASSERT_EQ(-1, addr);

	str = L"abcdef";
	addr = 333;
	result = Zaf::Net::FromString(str, addr);
	ASSERT_EQ(false, result);
	ASSERT_EQ(333, addr);
	result = Zaf::Net::FromString(L"abcef", addr);
	ASSERT_EQ(false, result);
	ASSERT_EQ(333, addr);
}